/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package components;

import entity.Appointment;
import entity.MedicalRecord;
import entity.Owner;
import entity.Pet;
import hpvas.HPVAS;
import java.text.SimpleDateFormat;
import java.util.List;
import javax.swing.DefaultListModel;

/**
 *
 * @author Juan
 */
public class POwnerDisplay extends javax.swing.JPanel {
    
    private Owner currentOwner = null;

    /**
     * Creates new form POwnerRegistration
     */
    public POwnerDisplay() {
        initComponents();
        initialize();
    }
    
    public POwnerDisplay(Owner owner) {
        this.currentOwner = owner;
        initComponents();
        initialize();
        LName.setText(owner.toString());
        LSSN.setText(owner.getSSN());
        SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
        LBirthday.setText(dateFormat.format(owner.getBirthday()));
        LBirthplace.setText(owner.getBirthplace());
        
        DefaultListModel petModel = new DefaultListModel();
        DefaultListModel chargeModel = new DefaultListModel();
        List<Pet> pets = owner.getPets();
        for (Pet pet : pets) {
            petModel.addElement(pet);
            chargeModel.addElement(calculateCharges(pet));
        }
        LPets.setModel(petModel);
        LCharges.setModel(chargeModel);
        LTotalCharges.setText("US$ " + String.valueOf(calculateTotalCharges()));
    }
    
    private int calculateCharges(Pet pet) {
        int charges = 0;
        MedicalRecord medicalRecord = pet.getMedicalRecord();
        List<Appointment> appointments = medicalRecord.getAppointments();
        for (Appointment appointment : appointments) {
            charges += appointment.getCharge();
        }
        return charges;
    }
    
    private int calculateTotalCharges() {
        int total = 0;
        DefaultListModel model = (DefaultListModel) LCharges.getModel();
        Object[] charges = model.toArray();
        for (int i = 0; i < charges.length; i++) {
            total += (int)charges[i];
        }
        return total;
    }
    
    private void initialize() {
        
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        LName = new javax.swing.JLabel();
        LSSN = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        Close = new javax.swing.JButton();
        BEditOwner = new javax.swing.JButton();
        LBirthday = new javax.swing.JLabel();
        LBirthplace = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        LTotalCharges = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        LPets = new javax.swing.JList();
        jScrollPane2 = new javax.swing.JScrollPane();
        LCharges = new javax.swing.JList();
        jLabel3 = new javax.swing.JLabel();
        BDeleteOwner = new javax.swing.JButton();

        LName.setText("OwnerName");

        LSSN.setText("SSN");

        jLabel4.setText("SSN:");

        jLabel5.setText("Birthday:");

        jLabel6.setText("Birthplace:");

        Close.setText("Close");
        Close.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CloseActionPerformed(evt);
            }
        });

        BEditOwner.setText("Edit Owner");
        BEditOwner.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BEditOwnerActionPerformed(evt);
            }
        });

        LBirthday.setText("Birthday");

        LBirthplace.setText("Birthplace");

        jLabel1.setText("Pets (Double click to see details):");

        jLabel2.setText("Total charges:");

        LTotalCharges.setText("US$");

        LPets.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        LPets.setToolTipText("Double click a pet to see details.");
        LPets.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                LPetsMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(LPets);

        LCharges.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jScrollPane2.setViewportView(LCharges);

        jLabel3.setText("Charges:");

        BDeleteOwner.setText("Delete Owner");
        BDeleteOwner.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BDeleteOwnerActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(Close)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(BEditOwner)
                        .addGap(18, 18, 18)
                        .addComponent(BDeleteOwner)
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel2)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(LTotalCharges))
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 268, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(LName)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jLabel4)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(LSSN))
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jLabel5)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(LBirthday))
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jLabel6)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(LBirthplace))
                                    .addComponent(jLabel1))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(jLabel3)
                                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 78, javax.swing.GroupLayout.PREFERRED_SIZE))))
                        .addGap(0, 152, Short.MAX_VALUE))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(LName)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(LSSN))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(LBirthday))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(LBirthplace))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 196, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 196, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(LTotalCharges)
                    .addComponent(jLabel2))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(Close)
                    .addComponent(BEditOwner)
                    .addComponent(BDeleteOwner))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void BEditOwnerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BEditOwnerActionPerformed
        HPVAS.editEntity(currentOwner);
    }//GEN-LAST:event_BEditOwnerActionPerformed

    private void CloseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CloseActionPerformed
        HPVAS.closeCurrentTab();
    }//GEN-LAST:event_CloseActionPerformed

    private void LPetsMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_LPetsMouseClicked
        if (evt.getClickCount() == 2 && LPets.getSelectedIndex() != -1) {
            HPVAS.displayEntity(LPets.getSelectedValue());
        }
    }//GEN-LAST:event_LPetsMouseClicked

    private void BDeleteOwnerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BDeleteOwnerActionPerformed
        HPVAS.deleteEntity(currentOwner);
    }//GEN-LAST:event_BDeleteOwnerActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton BDeleteOwner;
    private javax.swing.JButton BEditOwner;
    private javax.swing.JButton Close;
    private javax.swing.JLabel LBirthday;
    private javax.swing.JLabel LBirthplace;
    private javax.swing.JList LCharges;
    private javax.swing.JLabel LName;
    private javax.swing.JList LPets;
    private javax.swing.JLabel LSSN;
    private javax.swing.JLabel LTotalCharges;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    // End of variables declaration//GEN-END:variables
}
